package com.baizhi.shane.common.base;

import com.baizhi.shane.enums.ResultCode;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

/**
 * 借鉴自https://blog.csdn.net/HuHao_CSDN/article/details/105050258
 * @param <T>
 */
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> {  //该类为api,和web统一的返回格式标准化
    /**
     * 1.status状态值：代表本次请求response的状态结果。
     */
    private Integer status;
    /**
     * 2.response描述：对本次状态码的描述。
     */
    private String message;
    /**
     * 3.data数据：本次返回的数据。
     */
    private T data;
 
    /**
     * 成功，创建ResResult：没data数据
     */
    public static Result OK() {
        Result result = new Result();
        result.setResultCode(ResultCode.SUCCESS);
        return result;
    }
 
    /**
     * 成功，创建ResResult：有data数据
     */
    public static Result OK(Object data) {
        Result result = new Result();
        result.setResultCode(ResultCode.SUCCESS);
        result.setData(data);
        return result;
    }
 
    /**
     * 失败，指定status、desc
     */
    public static Result fail(Integer status, String desc) {
        Result result = new Result();
        result.setStatus(status);
        result.setMessage(desc);
        return result;
    }
 
    /**
     * 失败，指定ResultCode枚举
     */
    public static Result fail(ResultCode resultCode) {
        Result result = new Result();
        result.setResultCode(resultCode);
        return result;
    }
 
    /**
     * 把ResultCode枚举转换为ResResult
     */
    private void setResultCode(ResultCode code) {
        this.status = code.code();
        this.message = code.message();
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}